package com.heima.wemedia.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.feign.article.IArticleClient;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.StatisticsDto;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.DateUtils;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.service.WmStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: team
 * @description:
 * @author: 羿哥哥
 * @create: 2023-06-28 15:25
 **/
@Service
@Slf4j
@Transactional
public class WmStatisticsServiceImpl implements WmStatisticsService {
    @Autowired
    private IArticleClient articleClient;

    /*图文统计*/
    @Override
    public ResponseResult dismension(String beginDate, String endDate) {
        Map<String, Object> resultMap = new HashMap<>();

        //类型转换   字符串转换为date类型
        Date beginDateTime = DateUtils.stringToDate(beginDate);
        Date endDateTime = DateUtils.stringToDate(endDate);


        WmUser user = WmThreadLocalUtil.getUser();



        //点赞数量&收藏数量
        ResponseResult responseResult = articleClient.queryLikesAndConllections(user.getId(), beginDateTime, endDateTime);
        if (responseResult.getCode().equals(200)) {
            String res_json = JSON.toJSONString(responseResult.getData());
            Map map = JSON.parseObject(res_json, Map.class);
            resultMap.put("likesNum", map.get("likes") == null ? 0 : map.get("likes"));
            resultMap.put("collectNum", map.get("collections") == null ? 0 : map.get("collections"));
            resultMap.put("publishNum", map.get("newsCount") == null ? 0 : map.get("newsCount"));
        }


        return ResponseResult.okResult(resultMap);
    }

    /*分页*/
    @Override
    public ResponseResult newPage(StatisticsDto dto) {
        log.info("到这里了吗");
        //获取用户id
        WmUser user= WmThreadLocalUtil.getUser();
        dto.setWmUserId(user.getId());
        PageResponseResult responseResult =articleClient.newPage(dto);
        return responseResult;
    }
}
